package cn.yi.newsys.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.yi.newsys.pojo.Comment;
import cn.yi.newsys.pojo.News;
import cn.yi.newsys.pojo.PageBean;
import cn.yi.newsys.pojo.User;
import cn.yi.newsys.service.CommentService;
import cn.yi.newsys.service.impl.CommentServiceImpl;

public class CommentServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response) ;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter() ;
		response.setContentType("text/html;charset=UTF-8");
		String action = request.getParameter("action") ;
		if(action != null && action.length() > 0){
			if(action.equals("add")){
				this.addComment(request, response) ;
			}else if(action.equals("look")){
				this.showComment(request, response) ;
			}else{
				out.print("<script>alert('未知错误！');location.href='index.jsp';</script>") ;
			}
		}
		out.flush() ;
		out.close() ;
		
	}
	
	private void addComment(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		PrintWriter out = response.getWriter();
		// 获取 session 对象
		HttpSession session = request.getSession() ;
		// 获取当前登录用户
		User user = (User)session.getAttribute("User") ;
		if(user != null){
			// 获取评论内容
			String com = request.getParameter("comment") ;
			// 获取新闻对象
			News news = (News)session.getAttribute("news") ;
			// 获取新闻 ID
			int newsID = news.getNid() ;
			// 获取评论时间
			Date date = new Date() ;
			// 获取评论用户姓名
			String name = user.getUserName() ;
			// 创建评论对象
			Comment comment = new Comment(null, newsID, com, date, "127.0.0.1", name) ;
			// 创建评论逻辑对象
			CommentService cs = new CommentServiceImpl() ;
			// 添加新闻
			boolean flag = cs.save(comment) ;
			if(flag){
				out.print("<script>alert('评论成功！');location.href='donews?action=look&id="+newsID+"';</script>");
			}else{
				out.print("<script>alert('评论失败！');location.href='donews?action=look&id="+newsID+"';</script>");
			}
		}else{
			out.print("<script>alert('未登录用户，不能发表评论！');location.href='index.jsp';</script>");
		}		
		out.flush();
		out.close();
	}

	
	private void showComment(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		HttpSession session = request.getSession() ;
		// 当前页数为 1
		int page = 1 ;
		// 获取当前新闻页新闻对象
		News news = (News)session.getAttribute("news");
		// 创建评论业务逻辑对象
		CommentService cs = new CommentServiceImpl() ;
		// 获取总页数
		int total = cs.totalComment(news.getNid()) ;
		// 获取页数
		Object i = request.getParameter("pageno") ;
		if(i != null){
			page = Integer.valueOf(String.valueOf(i)) ;
		}
		// 设置上限
		if(page >= total){
			page = 1 ;
		}
		// 设置下限
		if(page <= 0){
			page = 1 ;
		}

		// 获取 pageBean
		PageBean<Comment> pb = cs.getCommentByNid(page, 5, news.getNid()) ;
		// 创建随机数生成对象
		Random random = new Random() ;
		// 生成随机数
 		int num = random.nextInt(4) ;	
		// 保存数据到 session 中
		session.setAttribute("comBean", pb) ;
		session.setAttribute("comTotal", total) ;	
		session.setAttribute("random", num) ;
		response.sendRedirect("newsread.jsp");
	}
}
